package com.xiaomage.view.draw13

import android.content.Context
import android.graphics.*
import android.view.View

/**
 * Created by Administrator on 2018/1/10.
 */
class MyView2(context: Context): View(context) {
    private var paint=getPaint()
    private var bitmap= Bitmap.createBitmap(500,
            500, Bitmap.Config.ARGB_8888)
    private var DSTBitmap: Bitmap =makeDST()
    private var SRCBitmap=makeSRC()
    init {
        paint=getPaint()
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        /*val c=Canvas(bitmap)
        c.drawText("XIAOMAGE",0f,100f,paint)
        canvas.drawBitmap(bitmap,0f,0f,paint)*/
      // canvas.drawBitmap(DSTBitmap,0f,0f,paint)
        canvas.drawColor(Color.BLACK)

        //val layerId=canvas.saveLayer(0f,0f,width.toFloat()
       // ,height.toFloat(),paint,Canvas.ALL_SAVE_FLAG)
        canvas.drawBitmap(DSTBitmap,0f,0f,paint)
        paint.xfermode= PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
        canvas.drawBitmap(SRCBitmap,100f,100f,paint)
        paint.xfermode=null
       // canvas.restoreToCount(layerId)


        val layerId=canvas.saveLayer(0f,0f,width.toFloat()
         ,height.toFloat(),paint, Canvas.ALL_SAVE_FLAG)
        canvas.skew(1.732f,0f)
        paint.color= Color.parseColor("#fc4c4f")
        canvas.drawRect(0f,0f,150f,160f,paint)
         canvas.restoreToCount(layerId)


    }
    private fun makeDST(): Bitmap {
        val bitmap= Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888)
        val c= Canvas(bitmap)
        paint.color= Color.parseColor("#448ced")
        c.drawCircle(100f,100f,100f,paint)
        return bitmap
    }
    private fun makeSRC(): Bitmap {
        val bitmap= Bitmap.createBitmap(
                300, 300, Bitmap.Config.ARGB_8888
        )
        val c= Canvas(bitmap)
        paint.color= Color.YELLOW
        c.drawRect(0f,0f,200f,200f,paint)
        return bitmap
    }


    private fun getPaint(): Paint {
        val paint= Paint()
        paint.isAntiAlias=true
        paint.color= Color.RED
        paint.style= Paint.Style.FILL_AND_STROKE
        paint.strokeWidth=5f
        paint.textSize=45f
        return paint
    }
}